<template>
  <div>
    <el-cascader-panel
      v-model="obj.pca"
      :options="pca"
      :props="{ label: 'name', value: 'name' }"
      @change="findData"
    ></el-cascader-panel>

    <div style="margin-top: 10px">
      <div style="width: 50%; float: left">
        <el-form ref="form" :model="obj" label-width="80px">
          <el-form-item label="确诊">
            <el-input v-model="obj.confirmed"></el-input>
          </el-form-item>
          <el-form-item label="疑似">
            <el-input v-model="obj.suspected"></el-input>
          </el-form-item>
          <el-form-item label="死亡">
            <el-input v-model="obj.dead"></el-input>
          </el-form-item>
          <el-form-item label="治愈">
            <el-input v-model="obj.cure"></el-input>
          </el-form-item>
          <el-form-item label="重症">
            <el-input v-model="obj.severe"></el-input>
          </el-form-item>
          <el-form-item label="境外输入">
            <el-input v-model="obj.outside"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="save">提交</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div style="width: 47%; float: right; color: red; font-weight: 800">
        <div>温馨提示：</div>
        <div>1. 先选择城市</div>
        <div>2. 再录入数据</div>
      </div>
    </div>
  </div>
</template>

<script>
import pca from "../../assets/pca.json";
import { mapActions, mapState } from "vuex";
import moment from "moment";

export default {
  data() {
    return {
      pca,
      obj: {
        country: "中国",
      },
    };
  },
  computed: {
    ...mapState("dataUpdate", ["dataList"]),
  },
  methods: {
    ...mapActions("dataUpdate", ["saveOrupdate", "page_query"]),
    // 查询某地区当天是否录入数据
    findData(val) {
      // val[2]
      // moment(new Date()).format('YYYY-MM-DD')
      let params = {
        page: 1,
        pageSize: 1,
        area: val[2],
        inputTime: moment(new Date()).format("YYYY-MM-DD"),
      };
      this.page_query(params).then(()=>{
          if(this.dataList.total > 0) {
            // 修改 
            // 思路类似于表格的修改：
            // 将当前行的数据放入模态框的表单
            this.obj = this.dataList.list[0];
          }
      })
    },
    // 数据上报
    save() {
      if(this.obj.pca) {
        this.obj.province = this.obj.pca[0];
        this.obj.city = this.obj.pca[1];
        this.obj.area = this.obj.pca[2];
        delete this.obj.pca;
      }
      this.saveOrupdate(this.obj).then((res) => {
        // 将res放入提示框
        console.log(res);
        this.$router.push({
          path: "/data_list/table",
        });
      });
    },
  },
};
</script>